package com.expense.util;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletContext;

import org.apache.log4j.Logger;

import com.expense.entity.RoleFunctionInfo;
import com.expense.entity.SysFunctionInfo;
import com.expense.entity.UserRoelInfo;
import com.expense.services.OperateInfoServices;

public class SkillTree { 
	public Logger logger = Logger.getLogger(this.getClass());
	private OperateInfoServices operateInfoServices=new OperateInfoServices();
	
	public int adminId=0;
	
	String functionTableName="";
	String role_functionTableName="";
		String user_roleTableName="";
	
	public  SkillTree(int adminIdValue){
		adminId=adminIdValue;
		
		
		 functionTableName=operateInfoServices.getTableName(SysFunctionInfo.class);
		
		 role_functionTableName=operateInfoServices.getTableName(RoleFunctionInfo.class);
		
		 user_roleTableName=operateInfoServices.getTableName(UserRoelInfo.class);
	}
	
	
	public String getTree() {
		
		
		
		String strTree = "";
		if(adminId == 0){
			return "用户错误";
		}
		//System.out.print("ss->"+admin.getLoginName());
		String sql = "select op.* from "+functionTableName+" op where op.OPERATETYPE=1   and  op.LEVELNUM=1 "+
			" and  op.SFID in ( "+
			"	select rpi.SFID from "+role_functionTableName+" rpi where rpi.RID in ("+
			"			select uri.RID from "+user_roleTableName+" uri where uri.UID="+adminId+
			"	)"+
			"  )"+
			" order by op.ORDERNO";
		 
		logger.info("hql -->" + sql);
		
		try{
			List<SysFunctionInfo> firstOperateList=operateInfoServices.selectList(sql,SysFunctionInfo.class);
			for(SysFunctionInfo operateInfo:firstOperateList)
			{
				String id = operateInfo.getSfid()+"";
				String menuName = operateInfo.getName();
				String funcUrl = operateInfo.getUrl();
				String target=operateInfo.getUrlTarget();
				
				
				int level=operateInfo.getLevelNum();
				
					//不是叶子
				
					if(funcUrl==null||funcUrl.equals("")){
						strTree += " <tr bordercolor=\"#000000\" class=\"s4\" > \n" + 
	 							"<td height='5%' bgcolor=#eeeeee ><font face=宋体 color=#333333>" + 
	 							 "<b>"+menuName+"</b></font></td>"+
	 							"</tr>";
					}else{
						if(funcUrl==null){funcUrl="";}
						if(target==null){target="";}
						strTree += " <tr bordercolor=\"#000000\" class=\"s4\" > \n" + 
							"<td height='5%' bgcolor=#eeeeee ><font face=宋体 color=#333333>" + 
							 "<b><a href=\"javascript:clickLeftUrl('"+funcUrl+"','"+target+"')\" >"+menuName+"</a></b></font></td>"+
							"</tr>";
					}
				  
					strTree += this.getChildTree(id,level);
			}
			 
		
		}catch(Exception e){
			logger.error("[Exception]="+e.toString());
	    }
		
		strTree += " <tr bordercolor=\"#000000\" class=\"s4\" > \n" + 
			"<td height='5%' bgcolor=#eeeeee ><font face=宋体 color=#333333>" + 
			 "<b><a href=\"javascript:clickLeftUrl('LoginLogout.servlet?opt=logout','_top')\" >退出管理系统</a></b></font></td>"+
			"</tr>";
		
		 
		
		return strTree;
	}
	
	/**
	 * 父级ID,父级级别
	 * */
	public String getChildTree(String fatID, int  fatlevel){
		
		
		
		
		String strChildTree="";
		//
		String sql = "select op.* from "+functionTableName+" op where op.OPERATETYPE=1   and  op.PARENTSFID="+fatID+
		"  and  op.SFID in ( "+
		"	select rpi.SFID from "+role_functionTableName+" rpi where rpi.RID in ("+
		"			select uri.RID from "+user_roleTableName+" uri where uri.UID="+adminId+
		"	)"+
		"  )"+
		" order by op.ORDERNO";
	 
		logger.info("Child "+fatID+" -->" + sql);
	
	try{
		List<SysFunctionInfo> operateList=operateInfoServices.selectList(sql,SysFunctionInfo.class);
		
		if(operateList.size()>0){
			
		}
		
		for(SysFunctionInfo operateInfo:operateList)
		{
			strChildTree+="<tr bordercolor=\"#000000\" class=\"s3\" >" +
			"<td height=\"20%\" bgcolor=#ffffff>" +
			"<table>" +
			"<tr bordercolor=\"#000000\" class=\"s3\"> "+
			"<td height=\"20%\" bgcolor=#ffffff>";
			
			String id = operateInfo.getSfid()+"";
			String menuName = operateInfo.getName();
			String funcUrl = operateInfo.getUrl();
			String target=operateInfo.getUrlTarget();
			int level=operateInfo.getLevelNum();
			
			for(int i=1;i<=fatlevel;i++){
				strChildTree +="&gt;&gt;";
			}
			
			if(funcUrl==null){funcUrl="";}
			if(target==null){target="";}
			//不是叶子
			strChildTree += "<a href=\"javascript:clickLeftUrl('"+funcUrl+"','"+target+"')\" >"+menuName+"</a> <br>";
			  
			strChildTree += this.getChildTree(id,level);
			
			strChildTree+="</td></tr></table></td></tr>";
		}
		
		if(operateList.size()>0){
			
		}
		
		 		 
	
	}catch(Exception e){
		logger.error("[Exception]="+e.toString());
    }
		 
		 
		return strChildTree;
	}
}
